package algorithm_learn.dataStructure;

import java.util.HashMap;
import java.util.TreeMap;

public class Code_HashMapTreeMap {

    //创建非原生类型！对象
    public static class Node{
        public int value;
        public Node(int value){
            this.value = value;
        }
    }

    public static void main(String[] args){
        HashMap<String ,String > map = new HashMap<>();
        map.put("lkc","I am KaiChuang Lin");
        System.out.println(map.containsKey("lkc"));
        //没有对应的键返回false
        System.out.println(map.containsKey("lkh"));
        //修改
        System.out.println(map.get("lkc"));
        map.put("lkc","changed KaiChuang Lin");
        System.out.println(map.get("lkc"));

        System.out.println("----------------------");
        // 比较
        HashMap<Integer,String > map2 = new HashMap<>();
        map2.put(123456,"我是123456");
        Integer a = 123456;
        Integer b = 123456;
        //使用 == 比较的是a 与 b 的内存地址！ 返回false！
        System.out.println(a==b);

        // Integer Double
        //返回的是同一个东西！ 原生类型 在map中都是按值传递！
        System.out.println(map2.get(a));
        System.out.println(map2.get(b));
        System.out.println("--------非原生类型验证---------");

        Node node1 = new Node(1);
        Node node2 = new Node(1);
        HashMap<Node,String> map3 = new HashMap<>();
        map3.put(node1,"哈哈哈哈哈");
        System.out.println("能不能查到node1 "+map3.containsKey(node1));
        System.out.println("能不能查到node2 "+map3.containsKey(node2));

        System.out.println("================================");
        TreeMap<Integer,String> treeMap = new TreeMap<>();
        treeMap.put(3,"我是3");
        treeMap.put(0,"我是3");
        treeMap.put(4,"我是3");
        treeMap.put(7,"我是3");
        treeMap.put(9,"我是3");

        System.out.println(treeMap.containsKey(0));
        System.out.println(treeMap.containsKey(4));
        System.out.println(treeMap.containsKey(7));
        System.out.println(treeMap.get(3));

        treeMap.put(3,"已经是变化后的3");
        System.out.println(treeMap.get(3));

        treeMap.remove(3);
        System.out.println(treeMap.get(3));
        //查找出最小的key
        System.out.println(treeMap.firstKey());
        //查找出最大的key
        System.out.println(treeMap.lastKey());

        // <= 离5最近的数字
        System.out.println("小于等于离5最近的数字 "+treeMap.floorKey(5));
        // <= 离6最近的数字
        System.out.println("小于等于离6最近的数字 "+treeMap.floorKey(6));
        // >= 离5最近的数字
        System.out.println("大于等于离5最近的数字 "+treeMap.ceilingKey(5));

        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        Node node7 = new Node(7);
        TreeMap<Node,String> treeMap2 = new TreeMap<>();
        treeMap2.put(node3,"我是node3");
        treeMap2.put(node4,"我是node4");
        treeMap2.put(node5,"我是node5");
        treeMap2.put(node6,"我是node6");
        treeMap2.put(node7,"我是node7");
        //直接放进去会报错 因为没有指定具体的比较方式



    }
}
